#include "server.hpp"

#define HOST "127.0.0.1"
#define USER "root"
#define PORT 3306
#define PASSWD "123456"
#define DBNAME "gobang"

void mysql_test()
{
    MYSQL* mysql = mysql_util::mysql_create(HOST, USER, PASSWD, DBNAME, PORT);
    const char* sql = "insert into stu values(null, '欧普七', 16, 55.6, 70, 80);";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if(ret == false)
        ELOG("mysql exec fail");
    mysql_util::mysql_destory(mysql);
}

void json_test()
{
    Json::Value root;
    root["姓名"] = "米哈游";
    root["年龄"] = 30;
    root["成绩"].append(60);
    root["成绩"].append(75.6);
    root["成绩"].append(94.2);
    std::string str;

    json_util::serialize(root, str);
    DLOG("%s", str.c_str());

    Json::Value val;
    json_util::unserialize(str, val);

    std::cout << "姓名: " << val["姓名"].asString() << std::endl;
    std::cout << "年龄: " << val["年龄"].asInt() << std::endl;
    int sz = val["成绩"].size();
    for(int i = 0;i < sz;i++)
    {
        std::cout << "成绩: " << val["成绩"][i].asFloat() << std::endl;
    }
}

void string_test()
{
    std::string src = "123,456,,,,,,789,,,";
    std::string sep = ",";
    std::vector<std::string> res;
    int n = string_util::spilt(src, sep, res);
    DLOG("%d", n);
    for(auto& e : res)
    {
        DLOG("%s", e.c_str());
    }
}

void file_test()
{
    std::string filename = "./Makefile";
    std::string str;
    file_util::read(filename, str);
    DLOG("%s", str.c_str());
}

void db_test()
{
    user_table ut(HOST, USER, PASSWD, DBNAME, PORT);
    Json::Value user;
    user["username"] = "qwe1";
    user["password"] = "1234561999";
    //int ret = ut.insert(user);
    //int ret = ut.login(user);
    //if(ret == false)
    //{
    //  DLOG("login fail");
    //}

    Json::Value val;
    std::string body;
    //bool ret = ut.select_by_name("qwe", val);

    // json_util::serialize(val, body);
    // ILOG("%s", body.c_str());

    bool ret = ut.lose(3);
    if(ret == false)
    {
        DLOG("win fail");
    }
   
}

void online_test()
{
    online_manager om;
    wssrv_t::connection_ptr con;
    om.enter_game_room(6, con);
    if(om.is_in_game_room(6))
    {
        ILOG("in game room");
    }
    else
    {
        ILOG("not in game room");
    }
    om.exit_game_room(6);
    if(om.is_in_game_room(6))
    {
        ILOG("in game room");
    }
    else
    {
        ILOG("not in game room");
    }
}

void room_test()
{
    user_table ut(HOST, USER, PASSWD, DBNAME, PORT);
    online_manager om;
    //room r(60, &ut, &om);
    room_manager rm(&ut, &om);
    room_ptr ptr = rm.create_room(100, 200);
}


int main()
{
    gobang_server _server(HOST, USER, PASSWD, DBNAME, PORT);
    _server.start(8877);
    return 0;
}